package loadTesting;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import managementClient.Client;

/**
 *
 * @author Group 95
 */
public class Tester 
{
    private int iClientCount;
    private int iAuctionsPerMinute;
    private int iAuctionsDuration;
    private int iUpdateIntervalSec;
    private int iBidsPerMinute;
    private String serverHost;
    private String tcpPort;
    private String analyticsBinding;
    private String billBinding;
    private boolean bStop;
    private long testStart;
    private TesterThread[] threads;
    
    public Tester(String serverHost,String tcpPort,String analyticsBinding,String billBinding)
    {
        bStop=false;
        this.serverHost=serverHost;
        this.tcpPort=tcpPort;
        this.analyticsBinding=analyticsBinding;
        this.billBinding=billBinding;
        this.testStart=System.currentTimeMillis();
    }

    /**
     * @return the iClientCount
     */
    public int getiClientCount() {
        return this.iClientCount;
    }
    
    /**
     * @return the iAuctionsPerMinute
     */
    public int getiAuctionsPerMinute() {
        return iAuctionsPerMinute;
    }

    /**
     * @return the iAuctionsDuration
     */
    public int getiAuctionsDuration() {
        return iAuctionsDuration;
    }

    /**
     * @return the iUpdateIntervalSec
     */
    public int getiUpdateIntervalSec() {
        return iUpdateIntervalSec;
    }

    /**
     * @return the iBidsPerMinute
     */
    public int getiBidsPerMinute() {
        return iBidsPerMinute;
    }    
    
    
    /**
     * @return the serverHost
     */
    public String getServerHost() {
        return serverHost;
    }

    /**
     * @return the tcpPort
     */
    public String getTcpPort() {
        return tcpPort;
    }

    /**
     * @return the analyticsBinding
     */
    public String getAnalyticsBinding() {
        return analyticsBinding;
    }      
    
    /**
     * @return the testStart
     */
    public long getTestStart() {
        return this.testStart;
    }      

    /**
     * @return the bStop
     */
    public boolean isbStop() {
        return bStop;
    }    
    
    public void loadServerProperties() throws Exception 
    {
        InputStream is = ClassLoader.getSystemResourceAsStream("loadtest.properties");
        if (is != null) {
            Properties tmp = new java.util.Properties();
            try {
                tmp.load(is);
                iClientCount = Integer.parseInt(tmp.getProperty("clients"));
                iAuctionsPerMinute = Integer.parseInt(tmp.getProperty("auctionsPerMin"));
                iAuctionsDuration = Integer.parseInt(tmp.getProperty("auctionDuration"));
                iUpdateIntervalSec = Integer.parseInt(tmp.getProperty("updateIntervalSec"));
                iBidsPerMinute = Integer.parseInt(tmp.getProperty("bidsPerMin"));
                
            } catch (IOException iox) {
                //Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, iox);
                throw new Exception("Error while reading Properties file");
            } finally {
                try {
                    is.close();
                } catch (IOException ex) {
                    //ignore
                }
            }
        } else 
        {
            // Logger.getLogger(Client.class.getName()).log(Level.SEVERE, "Properties file not found!");
            throw new Exception("Properties file not found!");
        }    
    }
    
    public void startManagement()
    {
        new Thread(new Runnable()
        {
            @Override
            public void run() {
                try {
                    Client client=new Client(analyticsBinding,billBinding);
                    client.setLoadTest();
                    client.start();
                    for(int i=0;i<iClientCount;i++)
                    {
                        threads[i].stop();
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    //  System.out.println("Couldn't start Management-Client");
                }
            }
            
        }).start();
    }
    
    public synchronized void startClients()
    {
        threads=new TesterThread[this.iClientCount];
        for(int i=0;i<this.iClientCount;i++)
        {
            try {
                threads[i]=new TesterThread(i,this);
                new Thread(threads[i]).start();
            } catch (IOException ex) {
                Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public void start() throws Exception
    {
        this.loadServerProperties();
        this.startManagement();
        this.startClients();
    }
    
    public static void main(String args[])
    {
        if(args.length!=4)
        {
            System.out.println ("Usage:"); 
            System.out.println ("java Tester <host> <tcpPort> <analytics.bindingname> <bills.bindingname>"); 
        }
        else
        {
            try 
            {
                new Tester(args[0],args[1],args[2],args[3]).start();
            }
            catch (Exception e) 
            {
                e.printStackTrace();
                System.err.println("Tester exception:");
            }                    
        }        
    }
}
